package org.goj.practice.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.goj.common.constant.ResultCode;
import org.goj.common.entity.practice.Solution;
import org.goj.common.model.RequestUtil;
import org.goj.common.model.Result;
import org.goj.practice.service.SolutionService;
import org.goj.practice.vo.SolutionContestVO;
import org.goj.practice.vo.SolutionSource;
import org.goj.practice.vo.StatusVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 程序运行结果记录 前端控制器
 * </p>
 *
 * @author CK
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/solution")
public class SolutionController {

    @Resource
    private SolutionService solutionService;

    /**
     * 插入一条待评测记录
     *
     * @param problemId 题目ID
     * @param userId    用户ID
     * @param source    代码
     * @param language  语言
     * @return Result<Integer>
     * @author CK
     * @date 2021/3/21 9:22
     */
    @PostMapping("/insertSolution")
    public Result<Integer> insertSolution(
            @RequestParam(value = "problemId") Integer problemId,
            @RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "source") String source,
            @RequestParam(value = "language") Integer language,
            @RequestParam(value = "num", required = false, defaultValue = "-1") Integer num,
            @RequestParam(value = "contestId", required = false, defaultValue = "0") Integer contestId) {
        Solution solution = new Solution();
        solution.setProblemId(problemId).setUserId(userId).setNick("Test")
                .setInDate(new Date()).setLanguage(language)
                .setIp(RequestUtil.request().getRemoteAddr())
                .setCodeLength(source.length()).setResult(0)
                .setNum(num).setContestId(contestId);
        int solutionId = solutionService.submit(solution, source);
        Assert.isTrue(solutionId > 0, "添加Solution失败！");
        return Result.succeed("添加Solution成功", solutionId);
    }

    /**
     * 提交后更新题目数据
     *
     * @param problemId 题目ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/3/25 21:37
     */
    @GetMapping("/updateSubmitProblem")
    public Result<Boolean> updateSubmitProblem(@RequestParam(value = "problemId") Integer problemId) {
        boolean flag = solutionService.updateSubmitProblem(problemId);
        Assert.isTrue(flag, "更新题目提交数据成功");
        return Result.succeed("更新题目提交数据成功");
    }

    /**
     * 前端根据提交ID轮询获取结果
     *
     * @param solutionId 记录ID
     * @return Result<Solution>
     * @author CK
     * @date 2021/3/21 9:23
     */
    @GetMapping("/resultBySolutionId")
    public Result<List<Solution>> resultBySolutionId(@RequestParam(value = "solutionId") Integer solutionId) {
        List<Solution> list = solutionService.result(solutionId);
        Assert.isTrue(list.size() == 1, "获取结果出错！");
        return Result.succeed("获取结果成功", list);
    }

    /**
     * 根据userId和problemId返回评测列表
     *
     * @param userId    用户ID
     * @param problemId 题目ID
     * @param contestId 竞赛ID
     * @return Result<List < Solution>>
     * @author CK
     * @date 2021/4/12 14:21
     */
    @GetMapping("/listResult")
    public Result<List<Solution>> listResult(
            @RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "problemId") Integer problemId,
            @RequestParam(value = "contestId", required = false, defaultValue = "-1") Integer contestId) {
        List<Solution> list = solutionService.result(userId, problemId, contestId);
        return Result.succeed("查询提交记录成功！", list);
    }

    /**
     * 条件查询提交状态
     *
     * @param current      当前页
     * @param size         尺寸
     * @param sort         排序方式
     * @param name         用户名或题目名
     * @param languageList 语言列表
     * @param listResult   结果列表
     * @return Result<Page < StatusVO>>
     * @author CK
     * @date 2021/4/12 14:55
     */
    @GetMapping("/listStatus")
    public Result<Page<StatusVO>> listStatus(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "sort", required = false, defaultValue = "8") Integer sort,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "languageList", required = false) List<Integer> languageList,
            @RequestParam(value = "listResult", required = false) List<Integer> listResult) {
        Page<StatusVO> page = solutionService.listStatus(current, size, sort, name, languageList, listResult);
        return Result.succeed("查询提交状态成功！", page);
    }

    /**
     * 查询提交的第三方代码
     *
     * @param solutionId 提交ID
     * @return Result<SolutionSource>
     * @author CK
     * @date 2021/4/12 18:32
     */
    @GetMapping("/solutionSourceById")
    public Result<SolutionSource> solutionSourceById(@RequestParam(value = "solutionId") Integer solutionId) {
        SolutionSource solutionSource = solutionService.solutionSourceById(solutionId);
        Assert.isTrue(solutionSource != null, "没有该提交详情");
        return Result.succeed("查询他人代码成功", solutionSource);
    }

    /**
     * 分页查询竞赛状态
     *
     * @param current   当前页
     * @param size      页面大小
     * @param contestId 竞赛ID
     * @return Result<Page < SolutionContestVO>>
     * @author CK
     * @date 2021/4/22 21:03
     */
    @GetMapping("/solutionContest")
    public Result<Page<SolutionContestVO>> solutionContest(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "contestId") Integer contestId) {
        Page<SolutionContestVO> page = solutionService.solutionContest(current, size, contestId);
        return Result.succeed("分页查询竞赛状态成功", page);
    }

}

